package com.example.myapplication.adapter;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.List;

/**
 * created by moxun on 2022/5/6
 * moxun@zhuangdianwenhua.com
 */
public class CustomPageAdapter extends PagerAdapter {

    private List<String> images;
    private Activity mActivity;

    public void setImages(List<String> images) {
        this.images = images;
    }

    public CustomPageAdapter(Activity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public int getCount() {
        return images == null ? 0 : images.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        ImageView roundedImageView = new ImageView(container.getContext());
        Glide.with(container.getContext())
                .load(images.get(position))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(roundedImageView);
//        ImageLoader.show(
//                ImageLoader.Builder.withString(container.getContext(), images.get(position))
//                        .noPlaceholderAndError()
//                        .listener(new RequestListener<Drawable>() {
//                            @Override
//                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                                enterTransition(roundedImageView);
//                                return false;
//                            }
//
//                            @Override
//                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                                enterTransition(roundedImageView);
//                                return false;
//                            }
//                        })
//                        .build(),
//                roundedImageView
//        );
        roundedImageView.setTransitionName(images.get(position));
        container.addView(roundedImageView);
        enterTransition(roundedImageView);
        return roundedImageView;
    }


    private void enterTransition(View view) {
        view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                view.getViewTreeObserver().removeOnPreDrawListener(this);
                mActivity.startPostponedEnterTransition();
                return true;
            }
        });
    }
}
